// state中写变量，相当于data,单一状态树（只建一个$state
import Vue from 'vue'
import { UPADTE_INFO,ACTIONS_UPADTE_INFO } from '../mutation-types'
const state = {
  counter:1000,
  students:[
    { id:110,name:'why',age:18 },
    { id:111,name:'kobe',age:24 },
    { id:112,name:'james',age:30 },
    { id:113,name:'curry',age:10 },
  ],
  info:{
    name:'kobe',
    age:40,
    height:'1.98'
  }
}

// 官方提倡使用 mutations 来改变状态，
// 这样是具体那个组件改的state中的值 devtools插件可以追踪到
// mutations中写方法,相当于methods
const mutations = {
  increment(state){
    state.counter++
  },
  decrement(state){
    state.counter--
  },
  // incrementCount(state,count){
  //   state.counter += count
  // }
  incrementCount(state,payload){
    state.counter += payload.count
  },
  [UPADTE_INFO](state){
    // state.info['address'] = '洛杉矶',
    // (object: object, key: string | number, value: "洛杉矶")
    Vue.set(state.info,'address','洛杉矶')
    // Vue.delete(state.info,'age')// 删除对象中的某个属性
  }
}

// 做一些异步操作，类似与mutations
const actions = {
  // context 上下文
  // [ACTIONS_UPADTE_INFO](context,payload){
  //   setTimeout(() => {
  //     console.log(context,payload);
  //     console.log(payload.message);
  //     // context.commit(UPADTE_INFO)
  //     payload.success()
  //   }, 1000);
  // }

  [ACTIONS_UPADTE_INFO](context,payload){
    return new Promise(resolve => {
      setTimeout(() => {
        console.log(payload);
        resolve('异步返回数据111')
      }, 1000);
    })
  }
}

export default {
  state,
  mutations,
  actions
}
